<?php


class connect extends CI_Model{
    
    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->library();
    }
     function checklogin($email, $pass) {
        $a_User = $this->db->select()->where('email', $email)->where('password', $pass)->get('users')->row_array();
        if (count($a_User) > 0) {
            return $a_User;
        } else {
            return false;
        }
    }
     function checkconversation($id1,$id2) {
         $where = "(user_id1=".$id1." AND user_id2=".$id2.") OR (user_id1=".$id2." AND user_id2=".$id1.")";
        $this->db->select('*');
         $this->db->from('conversations');
         
         $this->db->where($where);
         
         
     
        $kq = $this->db->get()->result();
        return $kq;
    }
    
    function  checkemail($email){
        $a_User = $this->db->select()->where('email', $email)->get('users')->row_array();
        if (count($a_User) > 0) {
            return $a_User;
        } else {
            return false;
        }
    }
     function getinfo($name) {
        $this->db->select('*');
        $this->db->where('email', $name);
     
        $kq = $this->db->get('users')->result();
        return $kq;
    }
    
    function search($a,$b,$c,$d,$e,$f,$g,$h,$i){
        
        $this->db->select('*');
        $this->db->like('gender',$a);
        $this->db->where('age >',$b);
        $this->db->where('age <',$c);
        $this->db->like('city',$d);
        $this->db->like('target',$e);
        $this->db->where('height >',$f);
        $this->db->where('height <',$g);
        $this->db->where('weight >',$h);
         $this->db->where('weight <',$i);
        $kq = $this->db->get('users')->result();
        return $kq;
    }
      function search1($a,$b,$c,$d){
        
        $this->db->select('*');
        $this->db->like('gender',$a);
        $this->db->where('age >',$b);
        $this->db->where('age <',$c);
        $this->db->like('city',$d);
        
        $kq = $this->db->get('users')->result();
        return $kq;
    }
    
    function searchcity($b){
         $this->db->select('*');
         
        $this->db->where('city',$b);
        
        $kq = $this->db->get('users')->result();
        return $kq;
    }
            function searchbox($kw) {
        $this->db->select('*');
        $this->db->like('name', $kw);
        $this->db->or_like('email',$kw);
        $this->db->limit(5);
        $kq = $this->db->get('users')->result();
        return $kq;
    }
             function getID($id) {

        $this->db->select('*');
        $this->db->where('user_id', $id);
        $kq = $this->db->get('users')->result();
        return $kq;
    }
    function countmessage($id){
        $this->db->distinct();
         $this->db->select('message.conversation_id');
         $this->db->from('message');
         $this->db->join('conversations', 'conversations.conversation_id = message.conversation_id');
         $this->db->where('message.receiver_id',$id);
          $this->db->where('message.status','Chưa xem');
         
          
     
        $kq = $this->db->get()->num_rows();
        return $kq;
    }
       function countconversation(){
         $this->db->select('*');
         $this->db->from('conversations');
         
         
     
        $kq = $this->db->get()->num_rows();
        return $kq;
    }
    function  getconversation($id){
        $this->db->select('*');
         $this->db->from('conversations');
         $this->db->where('user_id1',$id);
         $this->db->or_where('user_id2',$id);
       
        $kq = $this->db->get()->result();
        return $kq;
    }
    function getlastmessage($id){
        $this->db->select_max('message_id');
         $this->db->from('message');
         $this->db->join('conversations', 'conversations.conversation_id = message.conversation_id');
         $this->db->where('conversations.conversation_id',$id);
        
        $kq = $this->db->get()->result();
        return $kq;
    }
    function  checkownmessage($id){
         $this->db->select('*');
         $this->db->from('message');
       
         $this->db->where('sender_id',$id);
         $this->db->where('status','Chưa xem');
        $kq = $this->db->get()->result();
        return $kq;
    }
            function getmessage($id){
        $this->db->select('*');
         $this->db->from('message');
         $this->db->join('conversations', 'conversations.conversation_id = message.conversation_id');
         $this->db->where('conversations.conversation_id',$id);
         
        $kq = $this->db->get()->result();
        return $kq;
    }
    function  getmessagedetails($id){
        $this->db->select('*');
         $this->db->from('message');
       
         $this->db->where('message_id',$id);
        $kq = $this->db->get()->result();
        return $kq;
    }
    
    function  getrequest($id){
         $this->db->select('*');
         $this->db->from('request');
       
         $this->db->where('receiver_id',$id);
          $this->db->where('request_type',1);
         $this->db->where('status','Chưa xác nhận');
         $this->db->order_by('request_id','asc');
        $kq = $this->db->get()->result();
        return $kq;
    }
    function checkrequest($id1,$id2){
        $this->db->select('*');
         $this->db->from('request');
            $this->db->where('sender_id',$id1);
         $this->db->where('receiver_id',$id2);
         
         $this->db->where('request_type',1);
         
        $kq = $this->db->get()->result();
        return $kq;
    }
    
       function isfriend($id1,$id2) {
         $where = "(user_id1=".$id1." AND user_id2=".$id2.") OR (user_id1=".$id2." AND user_id2=".$id1.")";
        $this->db->select('*');
         $this->db->from('relationship');
         
         $this->db->where($where);
         
         
     
        $kq = $this->db->get()->result();
        return $kq;
    }
         function getfriend($id1) {
         $where = "(user_id1=".$id1." OR   user_id2=".$id1.")";
        $this->db->select('*');
         $this->db->from('relationship');
         
         $this->db->where($where);
         
         
     
        $kq = $this->db->get()->result();
        return $kq;
    }
    
       function requestSended($id1,$id2){
        $this->db->select('*');
         $this->db->from('request');
            $this->db->where('sender_id',$id1);
         $this->db->where('receiver_id',$id2);
          $this->db->where('status','Chưa xác nhận');
         
        $kq = $this->db->get()->result();
        return $kq;
    }
           function requestRefused($id1,$id2){
        $this->db->select('*');
         $this->db->from('request');
            $this->db->where('sender_id',$id1);
         $this->db->where('receiver_id',$id2);
          $this->db->where('status','Đã từ chối');
         
        $kq = $this->db->get()->result();
        return $kq;
    }
    function getalbum($id){
         $this->db->select('*');
         $this->db->from('album');
            $this->db->where('user_id',$id);
         
        $kq = $this->db->get()->result();
        return $kq;
    }
    function getmaxalbumid(){
        $this->db->select_max('album_id');
         $this->db->from('album');
        $kq = $this->db->get()->result();
        return $kq;
    }
            function getlastphoto($id){
         $this->db->select_max('photo_id');
         $this->db->from('photo');
            $this->db->where('album_id',$id);
         
        $kq = $this->db->get()->result();
        return $kq;
    }         function getphoto2($id){
        $this->db->select('*');
         $this->db->from('photo');
            $this->db->where('photo_id',$id);
         
        $kq = $this->db->get()->result();
        return $kq;
    }
            function getphoto($id){
        $this->db->select('*');
         $this->db->from('photo');
            $this->db->where('album_id',$id);
         
        $kq = $this->db->get()->result();
        return $kq;
    }
        function getdating2($a){
         $this->db->select('*');
         $this->db->from('dating'); 
           $this->db->where('dating_id',$a);
         
        $kq = $this->db->get()->result();
        return $kq;
    }
            function getdating(){
         $this->db->select('*');
         $this->db->from('dating');     
        $kq = $this->db->get()->result();
        return $kq;
    }
    function getfirstdating(){
         $this->db->select('*');
         $this->db->from('dating');
         $this->db->limit(4); 
         $this->db->order_by("dating_id", "desc"); 
         
        $kq = $this->db->get()->result();
        return $kq;
    }
            function loaddating($a,$b){
         $this->db->select('*');
        $this->db->from('dating');      
       $this->db->limit($a, $b);
       $this->db->order_by("dating_id", "desc"); 


        $kq = $this->db->get()->result();

        return $kq;
    }
    
     function searchdating($d,$e,$f,$g){
        
        $this->db->select('*');
        $this->db->from('dating');
         $this->db->join('users', 'dating.user_id1 = users.user_id');
        $this->db->like('users.gender',$d);
        $this->db->like('users.city',$e);
        $this->db->where('date_dating <',$f);
        $this->db->where('date_dating >',$g);
        
     
        $kq = $this->db->get()->result();
        return $kq;
    }
     function searchdating1($d){
        
        $this->db->select('*');
        
        $this->db->like('target',$d);
        
        $kq = $this->db->get('dating')->result();
        return $kq;
    }
     function searchdating2($d,$e){
        
         $this->db->select('*');
        $this->db->from('dating');
         $this->db->join('users', 'dating.user_id1 = users.user_id');
        $this->db->like('users.gender',$d);
        $this->db->like('users.city',$e);
      
        
     
        $kq = $this->db->get()->result();
        return $kq;
    }
     function searchdating3($d,$e,$f){
        
        $this->db->select('*');
          $this->db->from('dating');
         $this->db->join('users', 'dating.user_id1 = users.user_id');
        $this->db->like('users.gender',$d);
        $this->db->like('users.city',$e);
        $this->db->where('date_dating >',$f);
        $kq = $this->db->get()->result();
        return $kq;
    }
      function searchdating4($d,$e,$f){
        
         $this->db->select('*');
          $this->db->from('dating');
         $this->db->join('users', 'dating.user_id1 = users.user_id');
        $this->db->like('users.gender',$d);
        $this->db->like('users.city',$e);
        $this->db->where('date_dating <',$f);
        $kq = $this->db->get()->result();
        return $kq;
    }
    function  loadcomment($a){
            $this->db->select('*');
        $this->db->from('comment');      
       $this->db->where("dating_id",$a); 
       $this->db->order_by("comment_id", "desc"); 
        $kq = $this->db->get()->result();

        return $kq;
    }
     function  loadcomment1($a){
            $this->db->select('*');
        $this->db->from('comment');      
       $this->db->where("comment_id",$a); 

        $kq = $this->db->get()->result();

        return $kq;
    }
    function checknotification($a){
           $this->db->select('*');
        $this->db->from('notification');      
       $this->db->where("id_receive",$a); 
        $this->db->where("id_send !=",$a);
       $this->db->where("status","Chưa xem");
        $kq = $this->db->get()->result();

        return $kq;
    }
    
            
    function getnotification($a){
           $this->db->select('*');
        $this->db->from('notification');      
       $this->db->where("id_receive",$a); 
      
        $kq = $this->db->get()->result();

        return $kq;
    }
      function getnotification1($a,$b){
           $this->db->select('*');
        $this->db->from('notification');      
       $this->db->where("id_receive",$a); 
      $this->db->where("notice_id",$b);
      $this->db->where("status","Chưa xem");
        $kq = $this->db->get()->result();

        return $kq;
    }
       function getlistjoin($a,$b){
           $this->db->select('*');
        $this->db->from('joindating');      
       $this->db->where("user_id",$a); 
      $this->db->where("dating_id",$b);  
        $kq = $this->db->get()->result();

        return $kq;
    }
      function getlistjoin1($a){
           $this->db->select('*');
        $this->db->from('joindating');      
       
      $this->db->where("dating_id",$a);  
        $kq = $this->db->get()->result();

        return $kq;
    }
    
    function getmoigianhap(){
          $this->db->select('*');
        $this->db->from('users');      
       $this->db->order_by("user_id", "desc"); 
       $this->db->limit(20); 
        $kq = $this->db->get()->result();

        return $kq;
    }
       function gettraixinh(){
          $this->db->select('*');
        $this->db->from('users');      
       $this->db->where('gender','Nam');
       $this->db->limit(20); 
        $kq = $this->db->get()->result();

        return $kq;
    }
        function getgaidep(){
          $this->db->select('*');
        $this->db->from('users');      
       $this->db->where('gender','Nữ');
       $this->db->limit(20); 
        $kq = $this->db->get()->result();

        return $kq;
    }
    function  getgoiy($a,$d,$e,$f,$g){
           $this->db->select('*');
        $this->db->where('gender !=',$a);
       
        $this->db->where('city',$d);
        $this->db->where('target',$e);
        $this->db->where('age >',$f);
        $this->db->where('age <',$g);
        
        $kq = $this->db->get('users')->result();
        return $kq;
    }
    
}

?>
